// fast4j - Fast & Agile Service Tools for Java
// Copyright (C) 2007 Alexandre ROMAN
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

// $Id: AbstractGroovyApplicationCreator.java 51 2007-04-08 12:37:12Z alexandre.roman $

package fast4j.ace.impl;

import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import fast4j.ace.ApplicationCreator;
import fast4j.ace.ApplicationDescriptor;
import fast4j.ace.PropertyDescriptor;
import groovy.text.GStringTemplateEngine;
import groovy.text.Template;
import groovy.text.TemplateEngine;

/**
 * {@link ApplicationCreator} implementation, providing Groovy facilities to
 * generate application.
 * @author alexandre.roman
 */
public abstract class AbstractGroovyApplicationCreator extends
        AbstractApplicationCreator {
    /**
     * Write a file using a Groovy template.
     */
    protected void writeToFile(File file, String templatePath,
            Map<String, String> bindings) throws Exception {
        final TemplateEngine templateEngine = new GStringTemplateEngine();
        
        final URL templateUrl = getClass().getResource(templatePath);
        if (templateUrl == null) {
            throw new IllegalArgumentException("Failed to load template: "
                    + templatePath);
        }
        final Template template = templateEngine.createTemplate(templateUrl);
        
        log.debug("Write to file '{}' using template '{}'", file.getPath(),
            templatePath);
        // create an empty file (and directories)
        FileUtils.touch(file);
        
        final FileWriter writer = new FileWriter(file);
        try {
            // write template to file
            template.make(bindings).writeTo(writer);
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }
    
    /**
     * Create bindings for using Groovy, from application properties.
     */
    protected Map<String, String> createBindings(ApplicationDescriptor desc) {
        final Map<String, String> bindings = new HashMap<String, String>(desc
                .getProperties());
        
        // insert default values
        for (final PropertyDescriptor prop : supportedProperties) {
            if (!bindings.containsKey(prop.getName())) {
                bindings.put(prop.getName(), prop.getDefaultValue());
            }
        }
        return bindings;
    }
}
